"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""
from flask import request
from flask_restx import Resource, Namespace, fields

from do.user_do import UserDo
from service.user_service import UserService

user_ns = Namespace("user", description="用户管理")

user_service = UserService()


@user_ns.route("/login")
class LoginController(Resource):
    login_post_model = user_ns.model("login_post_model", {
        "username": fields.String,
        "password": fields.String
    })

    @user_ns.expect(login_post_model)
    def post(self):
        """
        登录功能
        :return:
        """
        user_info = request.json
        # 通过用户名和密码生成 user 对象
        user = UserDo(**user_info)
        # 通过用户名查询用户是否存在
        user_result = user_service.get_by_name(user.username)
        # 如果用户不存在，说明用户名未注册
        if not user_result:
            return {"code": 40013, "msg": "用户未注册"}
        # 如果密码不匹配
        if not user_result.check_hash_password(user_info.get("password")):
            # 返回提示信息与未授权的响应状态码
            return {"code": 40014, "msg": "密码错误"}
        # 用户存在，生成 token
        access_token = user_service.create_access_token(user_result.as_dict())
        if access_token:
            return {"code": 0, "msg": "登录成功", "data": {"token": access_token}}
        else:
            return {"code": 40021, "msg": "登录失败"}


@user_ns.route("/register")
class RegisterController(Resource):
    register_post_model = user_ns.model("register_post_model", {
        "username": fields.String,
        "password": fields.String,
    })

    @user_ns.expect(register_post_model)
    def post(self):
        """
        注册功能
        :return:
        """
        data = request.json
        user = UserDo(**data)
        user_id = user_service.save(user)
        if user_id:
            return {"code": 0, "msg": f"{user_id} success add"}
        else:
            return {"code": 40001, "msg": "user is exists"}
